// pages/card/card.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    data:{
      id:0,
      name:'祖国花朵',
      work:'默认工作岗位',
      num:'默认电话',
      wx:' 默认微信',
      mail:' 默认邮件',
      region:['河北省', '石家庄市', '裕华区'],
      place:'ceshi',
      about:'默认个人介绍',
      img:'/img/touxiang.png',
    },
    id:0,
  },

  bindRegionChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    // var region = e.detail.value.join(",");
    this.setData({
      'data.region':e.detail.value,
    })
  },

  inpName:function(e){
    var that = this;
    that.setData({
      'data.name':e.detail.value
    }) 
  },
  inpWork:function(e){
    var that = this;
    that.setData({
      'data.work':e.detail.value
    }) 
  },
  inpNum:function(e){
    var that = this;
    that.setData({
      'data.num':e.detail.value
    }) 
  },
  inpWx:function(e){
    var that = this;
    that.setData({
      'data.wx':e.detail.value
    }) 
  },
  inpMail:function(e){
    var that = this;
    that.setData({
      'data.mail':e.detail.value
    }) 
  },
  inpPlace:function(e){
    var that = this;
    that.setData({
      'data.place':e.detail.value
    }) 
  },


  btn:function(){
    var that = this;
    console.log(that.data.data)
    wx.request({
      url: `http://127.0.0.1:2020/api/change`,
      data:that.data.data,
      method:'GET',
      header:{'content-type':'application/JSON;charset=UTF-8'},
      success:(res)=>{
        console.log(res.data);
        wx.setStorageSync('users', that.data.data);
        wx.request({
          url: `http://127.0.0.1:2020/api/company?com_name=${that.data.data.place}`,
          method:'GET',
          header:{'content-type':'application/JSON;charset=UTF-8'},
            success:function(res){
            // console.log(res.data[0])
            wx.setStorageSync('user_com', res.data[0])
            }
        })
      }
    })
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let that= this;
    that.setData({
      id:wx.getStorageSync('user_id'),
    })
    wx.request({
      url: `http://127.0.0.1:2020/api/login?id=${that.data.id}`,
      method:'GET',
      header:{'content-type':'application/JSON;charset=UTF-8'},
      success:(res)=>{ 
        that.setData({
          data:res.data,
        })
        var region = that.data.data.region;
        region= region.split(',');
        region[0]=region[0].split('"')[1];
        region[1]=region[1].split('"')[1];
        region[2]=region[2].split('"')[1];
        that.setData({
          'data.region':region,
        })  
      }
    })
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})